<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:ext="http://exslt.org/common"
	xmlns:geo="http://www.ncbi.nlm.nih.gov/geo/info/MINiML"
	>
	
	<xsl:include href="URLUtils.xsl"/>
	
	<xsl:template match="/">
		<html>
			<head>
				<meta charset="UTF-8" />
				<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
				<title>GEO数据报告</title>
				<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />  
				<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
				<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
			</head>
			<body>
				<xsl:variable name="termStat">
					<xsl:call-template name="iterTerms">
						<xsl:with-param name="terms" select="ncbi/gds/terms" />
					</xsl:call-template>
				</xsl:variable>
				
				<xsl:call-template name="printTermStat">
					<xsl:with-param name="termStat" select="$termStat" />
				</xsl:call-template>
				
				<h1>标准</h1>
				<h2>纳入标准</h2>
				<h2>排除标准</h2>
				<ol>
					<li>Data of samples not explicitly from patient with ischemic stroke (tumor, cultured cells including stem cells and normal cells)</li>
					<li>Data of samples from animal model (mice, etc.)</li>
				</ol>
				<h1>表型</h1>
				<table>
					<thead>
						<tr>
							<th>数据库</th>
							<th>Accession</th>
							<th>文献语言</th>
							<th>疾病</th>
							<th>例数</th>
							<th>人种</th>
							<th>地区</th>
						</tr>
					</thead>
					<tbody>
						
					</tbody>
				</table>
			</body>
			</html>
	</xsl:template>
	
	<xsl:template name="iterTerms">
		<xsl:param name="terms" />
		
		<xsl:for-each select="$terms/term">
			<term>
				<!-- Do not tab in your URL or it won't work! -->
				<!-- Use of normalize-space function allows you to tab laterally but not internally! -->
				<name tag="检索词"><xsl:value-of select="child::node()" /></name>
				<xsl:variable name="encodedKWord">
					<xsl:call-template name="url-encode">
						<xsl:with-param name="str" select="child::node()"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:variable name="url-prefix">https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gds&amp;term=</xsl:variable>
				
				<!-- [] needs to be encoded as %5B%5D -->
				<xsl:variable name="species">
					<xsl:if test="/ncbi/gds/limits/species">
						<xsl:call-template name="url-encode"><xsl:with-param name="str" select="/ncbi/gds/limits/species"/></xsl:call-template>%5Borgn%5D+AND+
					</xsl:if>
				</xsl:variable>
				
				<xsl:variable name="url">
					<xsl:value-of select="concat($url-prefix,normalize-space($species),$encodedKWord)" />
				</xsl:variable>
				<xsl:variable name="search" select="document(normalize-space($url))/eSearchResult" />
				<test-URL tag="测试URL"><xsl:value-of select="$url" /></test-URL>
				<!-- 
				结果：<xsl:value-of select="$search" /><br />
				 -->
				<formula tag="检索式"><xsl:value-of select="$search/QueryTranslation" /></formula>
				
				<xsl:variable name="retmax">&amp;retmax=<xsl:value-of select="$search/Count" /></xsl:variable>
				
				<xsl:variable name="fullSearchURL" select="concat($url-prefix, normalize-space($species), $encodedKWord, $retmax)" />
				<xsl:variable name="fullSearch" select="document(normalize-space($fullSearchURL))/eSearchResult/IdList" />
				<uni-URL tag="正式URL"><xsl:value-of select="$fullSearchURL" /></uni-URL>
				<count>
					<total><xsl:value-of select="$search/Count" /></total>
					<GSE><xsl:value-of select="count($fullSearch/Id[starts-with(text(),'2')])" /></GSE>
					<GSM><xsl:value-of select="count($fullSearch/Id[starts-with(text(),'3')])" /></GSM>
				</count>
				
				<web>
					<xsl:call-template name="iterGEOs">
						<xsl:with-param name="general" select="$fullSearch" />
					</xsl:call-template>
				</web>
			</term>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="iterGEOs">
		<xsl:param name="general" />
		
		<xsl:for-each select="$general/Id">
			<xsl:choose>
				<!-- 3开头的ID是GSM -->
				<xsl:when test="starts-with(child::node(),'2')">
					<xsl:variable name="accession" select="number(substring(child::node(),2))" />
					<xsl:variable name="gsmURL">
						https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?targ=self&amp;view=full&amp;form=xml&amp;acc=GSE<xsl:value-of select="$accession" />
					</xsl:variable>
					<!-- We are not able to proceed with root node possessing xmlns attribute. Try to replace. -->
					<!-- The correct way is to add the namespace URL to our document and provide with our alias for that. Quote the alias whenever using the document -->
					<xsl:copy-of select="document(normalize-space($gsmURL))/geo:MINiML" />
				</xsl:when>
				<xsl:otherwise />
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="printTermStat">
		<xsl:param name="termStat" />
		<xsl:variable name="root" select="ext:node-set($termStat)/term/web/geo:MINiML" />
		<xsl:variable name="unique-set" select="$root/geo:Series[not(@iid = preceding::geo:Series/@iid)]" />
		
		<h1>总览</h1>
		<p>检索结果<xsl:value-of select="count($root)" />个，非重结果<xsl:value-of select="count($unique-set)" />个。</p>
		
		<xsl:call-template name="printExperimentTable">
			<xsl:with-param name="experiments" select="$root/geo:Series/geo:Type[not(.=preceding::geo:Type)]" />
			<xsl:with-param name="GSEs" select="$unique-set" />
		</xsl:call-template>
		
		<xsl:call-template name="printCountryTable">
			<xsl:with-param name="countries" select="$root/geo:Contributor[@iid='contrib1']/geo:Address/geo:Country[not(.=preceding::geo:Country)]" />
			<xsl:with-param name="contacts" select="$unique-set/ancestor::geo:MINiML/geo:Contributor[@iid='contrib1']/geo:Address"></xsl:with-param>
		</xsl:call-template>
		
		<xsl:call-template name="printCityTable">
			<xsl:with-param name="cities" select="$root/geo:Contributor[@iid='contrib1']/geo:Address/geo:City[not(.=preceding::geo:City)]" />
			<xsl:with-param name="contacts" select="$unique-set/ancestor::geo:MINiML/geo:Contributor[@iid='contrib1']/geo:Address" />
		</xsl:call-template>
		
		<xsl:call-template name="printGSETable">
			<xsl:with-param name="GSEs" select="$unique-set/ancestor::geo:MINiML" />
		</xsl:call-template>
		
		<h1>检索策略</h1>
		<xsl:for-each select="ext:node-set($termStat)/term">
			<h2><xsl:value-of select="name" /></h2>
			测试URL：<xsl:value-of select="test-URL" /><br />
			检索式：<xsl:value-of select="formula" />	<br />
			正式URL：<xsl:value-of select="uni-URL" /><br />
			检索到<xsl:value-of select="count/total" />个结果，GSE<xsl:value-of select="count/GSE" />例，GSM<xsl:value-of select="count/GSM" />例。
			<xsl:call-template name="printReport">
				<xsl:with-param name="stat" select="web" />
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="printReport">
		<xsl:param name="stat" />
		<xsl:variable name="root" select="ext:node-set($stat)" />
		<xsl:variable name="seriesInfos" select="$root/geo:MINiML/geo:Series" />
		
		<!-- 开始生成报表 -->
		<xsl:call-template name="printExperimentTable">
			<xsl:with-param name="experiments" select="$seriesInfos/geo:Type[not(.=ancestor::geo:MINiML/preceding-sibling::geo:MINiML/geo:Series/geo:Type)]" />
			<xsl:with-param name="GSEs" select="$seriesInfos" />
		</xsl:call-template>
		
		<xsl:variable name="contacts" select="$root/geo:MINiML/geo:Contributor[@iid='contrib1']/geo:Address" />
		
		<xsl:call-template name="printCountryTable">
			<xsl:with-param name="countries" select="$contacts/geo:Country[not(.=ancestor::geo:MINiML/preceding-sibling::geo:MINiML/geo:Contributor[@iid='contrib1']/geo:Address/geo:Country)]" />
			<xsl:with-param name="contacts" select="$contacts"></xsl:with-param>
		</xsl:call-template>
		
		<xsl:call-template name="printCityTable">
			<xsl:with-param name="cities" select="$contacts/geo:City[not(.=ancestor::geo:MINiML/preceding-sibling::geo:MINiML/geo:Contributor[@iid='contrib1']/geo:Address/geo:City)]" />
			<xsl:with-param name="contacts" select="$contacts" />
		</xsl:call-template>
		
		<xsl:call-template name="printGSETable">
			<xsl:with-param name="GSEs" select="$root/geo:MINiML" />
		</xsl:call-template>
	</xsl:template>
	
	<xsl:include href="TableUtils.xsl" />
	
	<xsl:output method="html" />
</xsl:stylesheet>